<template>
  <div class="phonics-alphabet-container">
    <el-container>
      <el-header class="hero">
        <h1>自然拼读 Phonics</h1>
        <p>通过系统性的音素学习，帮助您建立英语发音基础</p>
      </el-header>

      <el-main class="content">
        <div v-if="loading">
          <el-skeleton :rows="5" animated />
        </div>
        <div v-else-if="error">
          <el-alert :title="error" type="error" show-icon :closable="false" />
        </div>
        <div class="phonics-section" v-else>
          <div class="lessons-grid">
            <div v-for="lesson in lessons" :key="lesson.id" class="lesson-item">
              <div class="lesson-header">
                <span class="lesson-title">
                  <el-tag :type="lesson.title.indexOf('元音') > -1 ? 'primary' : 'warning'">
                    {{ lesson.title }}
                  </el-tag>
                </span>
              </div>
              <div class="lesson-content">
                <div class="sounds-grid">
                  <el-button v-for="sound in lesson.sounds" :key="sound.id"
                    :type="lesson.title.indexOf('元音') > -1 ? 'primary' : 'warning'" plain
                    @click="$router.push(`/phonics/${lesson.id}/${sound.id}?title=${encodeURIComponent(lesson.title)}`)"
                    class="sound-button">
                    {{ sound.id.indexOf('_') > -1 ? sound.id.replace('_', '(') + ')' : sound.id }}
                  </el-button>
                </div>
              </div>
            </div>
          </div>
        </div>
      </el-main>
    </el-container>
  </div>
</template>

<script>
import { ElContainer, ElHeader, ElMain, ElSkeleton, ElAlert, ElButton, ElTag } from 'element-plus'
import http from '@/utils/http';

export default {
  name: 'PhonicsAlphabet',
  components: {
    ElContainer,
    ElHeader,
    ElMain,
    ElSkeleton,
    ElAlert,
    ElButton,
    ElTag
  },
  data() {
    return {
      lessons: [],
      loading: true,
      error: null
    }
  },
  async mounted() {
    await this.loadLessons()
  },
  methods: {
    async loadLessons() {
      try {
        // 从后端获取完整的课程信息（包括sounds）
        const response = await http.get('/phonics/lessons');
        this.lessons = response;
        console.log('Lessons:', this.lessons.length);
        this.loading = false;
      } catch (err) {
        console.error('Error loading phonics lessons:', err);
        this.error = '加载课程时出错';
        this.loading = false;
      }
    }
  }
}
</script>

<style scoped>
.phonics-alphabet-container {
  padding: 0 0px;
}

.hero {
  text-align: center;
  height: auto !important;
  text-align: center;
  padding: 20px 20px !important;
  border-radius: 10px;
  background: linear-gradient(135deg,
      rgba(131, 184, 65, 0.7) 0%,
      rgba(131, 184, 65, 0.9) 100%),
    url('/assets/images/cover001.avif');
  background-size: cover;
  background-position: center;
  color: white;
  position: relative;
}

.hero h1 {
  font-size: 2.5rem;
  color: white;
  margin-bottom: 10px;
  font-family: 'DynaPuff';
}

.hero p {
  font-size: 1.1rem;
  color: #eeeeee;
}


.lessons-grid {
  display: flex;
  flex-direction: column;
  gap: 20px;
  margin-top: 20px;
}

.lesson-item {
  display: flex;
  align-items: center;
  padding: 5px;
  border-radius: 4px;
  border: 1px solid #EBEEF5;
  transition: box-shadow 0.2s;
}

.lesson-item:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.lesson-header {
  flex: 0 0 200px;
  padding-right: 20px;
  border-right: 1px solid #EBEEF5;
}

.lesson-title {
  font-size: 1.2rem;
  font-weight: bold;
  color: #303133;
}

.lesson-content {
  flex: 1;
  padding-left: 20px;
}

.sounds-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.sound-button {
  margin: 5px;
  font-family: "DynaPuff";
}

.content {
  padding: 10px 0 !important;
}

@media (max-width: 768px) {
  .lessons-grid {
    gap: 5px;
  }

  .lesson-item {
    flex-direction: column;
    align-items: flex-start;
  }

  .lesson-header {
    flex: 0 0 auto;
    padding-right: 0;
    padding-bottom: 5px;
    border-right: none;
    border-bottom: 1px solid #EBEEF5;
    width: 100%;
  }

  .lesson-title {
    display: block;
    text-align: center;
  }

  .lesson-content {
    flex: 1;
    padding-left: 0;
    padding-top: 15px;
    width: 100%;
  }

  .sounds-grid {
    justify-content: center;
    gap: 0px;
  }

  .sound-button {
    margin: 5px 0;
  }

  .content {
    padding: 10px 0 !important;
  }

  .hero {
    padding: 15px;
  }

  .hero h1 {
    font-size: 1.5rem;
  }

  .hero p {
    font-size: 1rem;
  }
}
</style>